import java.io.*;
import java.util.ArrayList;
import javax.swing.*;

@SuppressWarnings("serial")
public class PentHighScore implements Serializable  {

    //creates the HighScore object
    public PentHighScore()
    {

        File highScoreFile = new File("PentHighscore.data");


        if (highScoreFile.exists())
        {
            try
            {
                in = new ObjectInputStream(new FileInputStream(highScoreFile));
                HighScoreList = (ArrayList)in.readObject();
            }
            catch(FileNotFoundException e)
            {
                System.out.println("Error: highscore file not found");
            }
            catch(ClassNotFoundException e)
            {
                System.out.println("Error: possibly corrupted highscore file");
            }
            catch(IOException e)
            {
                System.out.println("Error opening highscore file");
            }
        }
        else 
        {
            Object HighScoreList = new ArrayList();
        }

    }


    //gets the current top score
    public int getHighScore() 
    {       
        int maximum = 0;

        if (HighScoreList.size() == 0)
            return maximum;

        else 
        {
            for ( int i = 0; i < HighScoreList.size(); i++)
            {
                Player temp = (Player)HighScoreList.get(i);

                if(temp.getScore() > maximum)
                {
                    maximum = temp.getScore();
                    topPlayer = temp;
                }
            }
            return maximum;
        }
    }

    //gets the name of the player with current highscore
    public String getHighScoreName()
    {
        int dummie = this.getHighScore();
        if (topPlayer == null)
            return "Someone! Play me to get a record!";
        else return topPlayer.getName();
    }

    // it checks if the new score is a highscore
    public void newHighScore(int aHighScore)
    {
        if (HighScoreList.size() == 0)
            addNewPlayer(aHighScore);

        else
        {
            int minimum = Integer.MAX_VALUE;

            for (int i = 0; i < HighScoreList.size(); i++)
            {
                Player temp = (Player)HighScoreList.get(i);

                if (temp.getScore() < minimum)
                {
                    minimum = temp.getScore();
                    worstPlayer = temp;
                    worstPlayerIndex = i;
                }
            }
            if (HighScoreList.size() < 10)
                addNewPlayer(aHighScore);
            else if (aHighScore > worstPlayer.getScore())
                addNewPlayer(aHighScore);
        }
    }

    //adds a new highscore to the list
    public void addNewPlayer(int aHighScore) 
    {
        String aName = JOptionPane.showInputDialog(" Awesome Highscore, type your name: ");
        Player newPlayer = new Player(aName, aHighScore);

        if (HighScoreList.size() < 10) 
            HighScoreList.add(newPlayer);
        else
            HighScoreList.set(worstPlayerIndex, newPlayer);

        try
        {
            out = new ObjectOutputStream(new FileOutputStream(HighScoreFile));
            out.writeObject(HighScoreFile);
        }
        catch(FileNotFoundException e)
        {
            System.out.println("Error: highscore file not found");
        }
        catch(IOException e)
        {
            System.out.println("Error writing highscore file");
        }               
    }
    // A string containing the top 10 Highscores
    public String getTopTen()
    {
        String topTen = "";
        ArrayList sortedFile = this.sort();
        for (int i = 0; i < sortedFile.size(); i++)
        {
            Player temp = (Player)sortedFile.get(i);
            topTen = topTen + (i + 1) + ". " + temp.getName() + ": " + temp.getScore() + "\n";
        }
        return topTen;
    }

    // sorts the highscores to get the the highest score first
    public ArrayList sort()
    {
        ArrayList sorted = new ArrayList();

        for (int i = 0; i < HighScoreList.size(); i++)
        {
            if (i == 0)
            {
                sorted.add(HighScoreList.get(i));
            }
            else
            {
                int sortedSize = sorted.size();
                int k = 0;
                boolean done = false;

                while ((!done) && (k < sortedSize))
                {
                    Player current = (Player)HighScoreList.get(i);

                    if(current.isLarger((Player)sorted.get(k)))
                    {
                        sorted.add(k, HighScoreList.get(i));
                        done = true;
                    }
                    else if (k ==sorted.size() - 1)
                        sorted.add(HighScoreList.get(i));
                    k++;
                }
            }
        }
        return sorted;
    }

    //closes the highscore table
    public void close()
    {

        try
        {
            out.close();
        }
        catch (IOException e)
        {
            System.out.println("Error: cannot close highscore window");
        }

    }

    private int worstPlayerIndex = 0;
    private File HighScoreFile;
    private Player topPlayer;
    private Player worstPlayer;
    private ArrayList HighScoreList;
    private ObjectInputStream in;
    private ObjectOutputStream out;
}

